<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>

<head>
<title>Click Button Click Option Login User Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  input { width: 20px; }
  input[id="password"] { width: 120px; }
  input[id="username"] { width: 120px; }
  /* 160 + 160 + 160 + 50 + 50 */
  #wrap,
  #sync-task-cover {
    height: 580px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 94px;
    padding: 3px;
  }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../common/ui_utils.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // set episode interval to 2000 seconds

var buttons = ['submit', 'okay', 'ok', 'yes', 'no', 'cancel', 'next', 'previous'];

// click-option
var createElements = function(div){
  var n = core.randi(2, 7);
  var clickNames = [];
  for(var i=0;i<n;i++) {
    var chname = ui_utils.generateString(2,8);
    var label = div.append('label')
    label.append('input').attr('type', 'radio').attr('id', 'ch'+i).attr('name', 'radio');
    label[0][0].innerHTML += chname;
    div.append('br');
    clickNames.push(chname);
  }
  var ix = core.randi(0, n);
  var qstr = clickNames[ix];
  return {query: qstr, index: ix};
}

var genProblem = function () {
  var task_state = -1.0
  var div = d3.select('#area');
  div.html(''); // clear previous problem, if any

  // click-button
  var num = 6;
  var elements = [];
  for (var i = 0; i < num; i++) {
    var u = core.randf(0, 1);
    var must_make_button = (i === num - 1) && elements.length === 0;

    if (u < 0.33 && !must_make_button) {
      var txt = ''; for (var q = 0; q < 3; q++) { txt += ' ' + core.sample(ui_utils.lorem_words); }
      div.append('div').html(txt);
    } else if (u < 0.66 && !must_make_button) {

      if (core.randf(0, 1) < 0.5) {
        var txt = ''; for (var q = 0; q < 3; q++) { txt += ' ' + core.sample(ui_utils.lorem_words); }
        txt += ': ';
        div.append('span').html(txt);
      }

      var w = core.randi(40, 150);
      div.append('input').attr('type', 'text').attr('style', 'width:' + w + 'px;');
      div.append('br');
    } else {
      var btn_text = core.sample(buttons);
      if (core.randf(0, 1) < 0.5) btn_text = ui_utils.txtCapitalize(btn_text);
      var btn = div.append('button').html(btn_text);
      if (core.randf(0, 1) < 0.5) div.append('br');
      elements.push(btn);
    }
  }
  var correct_text = core.sample(elements).html();

  // reward
  for (var i = 0, len = elements.length; i < len; i++) {
    var e = elements[i];
    if (e.html() === correct_text) {
      e.on('click', function () { task_state = 1.0; console.log([task_state, correct_text]);})
    } else {
        e.on('click', function () { task_state = -1.0; console.log([task_state, correct_text]);})
    }
  }

  // click-option
  div.append('br')
  div.append('div').attr('id', 'boxes')
  var div_option = d3.select('#boxes');
  div_option.html('');
  var correctElement = createElements(div_option);
  div.append('br')

  // login-user
  div.append('div').attr('id', 'form');
  var form = d3.select('#form');
  form.html('<p><label class="bold">Username</label><input type="text" id="username"></p><p><label class="bold">Password</label><input type="password" id="password"></p><button id="subbtn" class="secondary-action">Login</button>');
  d3.select('#username')[0][0].value ='';
  d3.select('#password')[0][0].value ='';

  var user = core.sample(ui_utils.FIFTY_NAMES).toLowerCase();
  var password = ui_utils.generateString(2,6);

  // d3.select('#query').html('Click on the "' + correct_text + '" button, select ' + correctElement.query + ', and then enter the <span class="bold">username</span> "' + user + '" and the <span class="bold">password</span> "' + password + '" into the text fields and press login.');
  d3.select('#query').html('Select ' + correctElement.query + ', and enter the <span class="bold">username</span> "' + user + '" and the <span class="bold">password</span> "' + password + '" into the text fields and press login, after clicking on the "' + correct_text + '" button.');

  // reward awarder
  d3.select('#subbtn').on('click', function(){
    var option_r = d3.select('#ch'+correctElement.index)[0][0].checked ? 1.0 : -1.0;
    console.log('click-option', option_r);
    var u = d3.select('#username')[0][0].value;
    var p = d3.select('#password')[0][0].value;
    var r = (u === user && p === password) ? 1.0 : -1.0;
    console.log('login-user', r);
    if (task_state == 1.0 && option_r == 1.0) {
      core.endEpisode(r, r > 0);
    } else {
      core.endEpisode(-1.0);
    }
  });
}

window.onload = function () {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
  <div id="wrap">
    <div id="query"></div>
    <div id="area"></div>
  </div>
</body>
</html>
